package com.yjd.job.shettlement;

import com.alibaba.edas.schedulerX.ProcessResult;
import com.alibaba.edas.schedulerX.ScxSimpleJobContext;
import com.alibaba.edas.schedulerX.ScxSimpleJobProcessor;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.funds.model.FundsPfModel;
import com.yjd.comm.funds.service.IFundsPfService;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 平台资金余额清算
 * Created by yjd-wap on 2017/11/15.
 */
public class FundsPfClearJob implements ScxSimpleJobProcessor {

    Logger logger = Logger.getLogger(FundsPfClearJob.class);

    @Override
    public ProcessResult process(ScxSimpleJobContext scxSimpleJobContext) {
        try {
            Long currtime = FrameUtil.getTime();
            Long start_time = 1507996800L;//默认从2017年10月15日开始统计
            Double begin_balance = 0d; //默认初始化余额为0
            Long end_time = DateUtil.getFristSecond(currtime - 86400);
            FundsPfModel fundsPfModel = fundsPfService.getModelOne(FrameUtil.newHashMap("sort", " clear_date desc "), DbWREnums.READ);
            if (fundsPfModel != null) {
                Date next_date = DateUtil.addDay(DateUtil.parseDate(fundsPfModel.getClear_date() + "", DateUtil.PATTERN_yyyyMMdd), 1);
                start_time = DateUtil.getParseDateStartTime(DateUtil.format(next_date, DateUtil.PATTERN_yyyyMMdd));
                begin_balance = fundsPfModel.getBalance();
            }
            logger.info("平台资金余额清算日期:" + start_time + "-" + end_time + "开始");
            fundsPfService.clearBalance(start_time, end_time, begin_balance);
            logger.info("平台资金余额清算日期:" + start_time + "-" + end_time + "结束");
        } catch (Exception e) {
            logger.error("平台资金余额清算异常，错误信息：" + e);
        }
        return new ProcessResult(true);
    }

    @Resource
    private IFundsPfService fundsPfService;
}
